package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.*;
import com.service.impl.CriticServiceimpl;
import com.service.impl.OrderServiceimpl;
import com.service.impl.UserRoleServiceimpl;
import com.service.impl.UserServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Zhengjz
 * @version 1.0
 * @className OrderController
 * @date 2021/3/1 17:36
 */
@Controller
public class OrderController {

    @Autowired
    private OrderServiceimpl orderServiceimpl;
    @Autowired
    private UserServiceimpl userServiceimpl;
    @Autowired
    private CriticServiceimpl criticServiceimpl;
    @Autowired
    private UserRoleServiceimpl userRoleServiceimpl;

    //购买套餐
    @RequestMapping("/orderinfo")
    public String orderInfo(){



        return "raising/order";
    }

    //饲养师的订单展示(套餐展示)
    @RequestMapping("/orderlist")
    public String orderList(HttpServletRequest request,Integer pageNum){
        if (pageNum == null) {
            pageNum = 1;
        }
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());



        //当前第几页，每页显示5条数据
        PageHelper.startPage(pageNum, 5);
        List<Order> orders = orderServiceimpl.rfindOrder(byId.getId());
        PageInfo pageTeacher = new PageInfo<>(orders);
     //   orders.forEach(item->System.out.println("订单id:"+item.getId()));
        request.setAttribute("list",pageTeacher);
        return "order/orderlist";
    }




    //饲养时查看订单评价
    @RequestMapping("/rpingjia")
    public String rPingJia(HttpServletRequest request){
        String orderId = request.getParameter("id");
        System.out.println("订单id:"+orderId);
//        User user = (User) request.getSession().getAttribute("user");
//        User byId = userServiceimpl.findById(user.getUserName());
//        RoleUser role = userRoleServiceimpl.findRole(byId.getId());
        List<Critic> critic = criticServiceimpl.findCritic(orderId);
        request.setAttribute("list",critic);
        request.setAttribute("orderId",orderId);

        return "order/rping";
    }


    //饲养师 完成订单
    @PostMapping("/rrrorder")
    public void rrrOrder(String id,HttpServletResponse response) throws IOException {

        int i = orderServiceimpl.rrrOrder(id);
        response.getWriter().println(i);
    }
    //用户买套餐
    @PostMapping("/addorder")
    public void addOrder(Order order, HttpServletResponse response, HttpServletRequest request) throws IOException {
        System.out.println("订单:"+order);

        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        order.setUserId(byId.getId());
        order.setStatus(0);
        int i = orderServiceimpl.addOrder(order);
        response.getWriter().println(i);
    }

    //查看我的订单
    @RequestMapping("/meorder")
    public String meOrder(HttpServletRequest request, Model model,Integer pageNum){
        if (pageNum == null) {
            pageNum = 1;
        }

        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        //当前第几页，每页显示5条数据
        PageHelper.startPage(pageNum, 5);
        List<Order> order = orderServiceimpl.findOrder(byId.getId());
        PageInfo pageTeacher = new PageInfo<>(order);
        model.addAttribute("list",pageTeacher);
      //  model.addAttribute("userId",byId.getId());
        return "order/meorder";
    }

    //评论页面
    @RequestMapping("/pinglun")
    public String pingLun(HttpServletRequest request){
        String orderId = request.getParameter("id");
        System.out.println("订单id:"+orderId);
//        User user = (User) request.getSession().getAttribute("user");
//        User byId = userServiceimpl.findById(user.getUserName());
//        RoleUser role = userRoleServiceimpl.findRole(byId.getId());
        List<Critic> critic = criticServiceimpl.findCritic(orderId);
        request.setAttribute("list",critic);
        request.setAttribute("orderId",orderId);
//        request.setAttribute("role",role.getRoleId());


        return "order/ping";
    }

    //添加用户评论
    @PostMapping("/addcritic")
    public void addCritic(Critic critic,HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("评论："+critic);
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        critic.setUserId(byId.getId());
        int i = criticServiceimpl.addPingLun(critic);
        response.getWriter().println(i);

    }


    //跳转饲养师发布套餐页面
    @RequestMapping("/raddorder")
    public String raddOrder(){
        return "order/raddorder";
    }

    //添加饲养师发布的套餐
    @PostMapping("/rraddorder")
    public void addOrder(String tuozahan,HttpServletResponse response,HttpServletRequest request) throws IOException {
      //  int i = orderServiceimpl.raddOrder(tuozahan);
        User user = (User) request.getSession().getAttribute("user");
        User byId = userServiceimpl.findById(user.getUserName());
        //通过userId查询出饲养师的id
        Raising raising = orderServiceimpl.selRa(Integer.toString(byId.getId()));
        Order order = new Order();
        order.setTuozhan(tuozahan);
        order.setSiyangId(raising.getId());
        //写入饲养师的id 和拓展套餐名称
        int i1 = orderServiceimpl.addRaId(order);
        response.getWriter().println(i1);
    }






}
